import {Component, OnInit} from '@angular/core';
import {BasePage} from '../../common/basePage';
import {ModalController, NavController, ToastController} from '@ionic/angular';
import {ActivatedRoute} from '@angular/router';
import {HttpService} from '../../services/http.service';
import {UrlAndOPT} from '../../common/url-and-opt.enum';
import {IframePageComponent} from '../../components/iframe-page/iframe-page.component';

/**
 * 我的出借－出借详情
 */
@Component({
  selector: 'app-user-lend-detail',
  templateUrl: './user-lend-detail.page.html',
  styleUrls: ['./user-lend-detail.page.scss'],
})
export class UserLendDetailPage extends BasePage implements OnInit {

  public data: any = {};

  constructor(
    public navCtrl: NavController,
    public toastCtrl: ToastController,
    private modalCtrl: ModalController,
    private routeInfo: ActivatedRoute,
    private service$: HttpService
  ) {
    super(navCtrl, toastCtrl);
  }

  ngOnInit() {
    // 接收列表传递过来的借款详情数据
    this.data = JSON.parse(this.routeInfo.snapshot.params['data']);
    console.log(this.data);
  }
  // 查看合同信息
  async openContract() {
    if (this.data['status'] === 4 || this.data['status'] === 5) { // 状态为4(还款中)或5(已还款)时才可查看合同信息
      const params = await this.service$.getUserInfoAndDeviceType({
        type: 2,
        sign: this.data['investId']
      });
      const modal = await this.modalCtrl.create({
        component: IframePageComponent,
        componentProps: {
          title: '合同信息',
          url: this.HOST + UrlAndOPT.USER_CONTRACT + this.service$.paramsSerialize(params)
        }
      });
      return await modal.present();
    } else {
      this.showToast('暂无合同信息');
    }
  }

}
